package com.cs410.collabwriting.tools;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigProperties {
	
	private Properties e_configProperty;
	private InputStream e_configReader;
	
	public ConfigProperties( String fileName ) {
		LogManager.logInfo("[ConfigProperties] -- Reading configuration parameters for file : " + fileName );
		try {
			e_configProperty = new Properties();
			e_configReader = ConfigProperties.class.getClassLoader().getResourceAsStream( fileName );
			e_configProperty.load( e_configReader );
		} catch ( NullPointerException exception ) {
			LogManager.logError("[ConfigProperties] -- " + exception.getMessage() );
		} catch ( IOException exception ) {
			LogManager.logError("[ConfigProperties] -- " + exception.getMessage() );
		}
	}
	
	public String getValue( String param ) {
		LogManager.logDebug("[ConfigProperties] -- Read value for Parameter " + param + " : " + e_configProperty.getProperty( param ), LogManager.LOG_LEVEL_2 );
		return e_configProperty.getProperty( param );
	}
	
	public void close() {
		try {
			e_configReader.close();
		} catch ( IOException exception ) {
			LogManager.logError("[ConfigProperties] -- " + exception.getMessage() );
		}
	}
	
	public static String getConfigValue( String fileName, String param ) {
		Properties configProperty = new Properties();
		String configValue = null;
		try {
			InputStream configReader = ConfigProperties.class.getClassLoader().getResourceAsStream(fileName );
			configProperty.load( configReader );
			configValue = configProperty.getProperty( param );
			LogManager.logDebug("[ConfigProperties] -- Read value for Parameter " + param + " : " + configValue, LogManager.LOG_LEVEL_2 );
			configReader.close();
		} catch ( NullPointerException exception ) {
			LogManager.logError("[ConfigProperties] -- " + exception.getMessage() );
		} catch ( IOException exception ) {
			LogManager.logError("[ConfigProperties] -- " + exception.getMessage() );
		}
		return configValue;
	}

}
